<?php

// +----------------------------------------------------------------------
// | 发送短信
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\event\ucenter;

use app\common\event\Base;
use think\Exception;
use think\facade\Event;
use think\facade\Session;
use app\model\ucenter\Sms as SmsModel;

class Sms extends Base
{

    /**
     * 发送短信
     * @param      $param
     * @param null $user
     * @return mixed
     * @author  : 微尘 <yicmf@qq.com>
     * @datetime: 2019/2/15 11:52
     */
    public static function send($param, $user = null)
    {
        try {
            $insert = [];
            $insert['mobile'] = $param['mobile'];
            $insert['data'] = $param;
            $insert['type'] = $param['type'];
            $insert['code'] = isset($param['code']) ? $param['code'] : '';
            $insert['result_code'] = 1;
            $insert['status'] = 1;
            $sms = SmsModel::create($insert);
            if (!$sms) {
                throw new  Exception('创建记录异常');
            }
            $data['code']  = 0;
            if (request()->client != 'test')
            {
                $data = Event::trigger('send_sms', $sms, true);
                if (0 == $data['code']) {
                    $sms['send_time'] = time_format();
                    $sms['channel'] = $data['channel'];
                    $sms['out_id'] = $data['out_id'];
                }
                $sms['result_code'] = $data['code'];
                $sms->save();
            }else{
                $data['data']['code'] = $sms['code'];
            }
//                $sms['result_message'] = $data['message'];
            $data['data']['tips'] = '发送成功';
            $data['data']['token'] = $sms['token'];
//            $data['data']['code'] =$sms['code'];
        } catch (Exception $e) {
            $data['code'] = 1;
            $data['message'] = $e->getMessage();
        }
        return $data;
    }

}
